package com.lzy.cloud.order.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.cloud.order.dtos.OrderInfoCarRentDto;
import com.lzy.cloud.order.dtos.rvcar.CarRentOrderDto;
import com.lzy.cloud.order.facade.OrderInfoFacade;
import com.lzy.cloud.order.remote.BaseApi;
import com.lzy.cloud.order.request.OrderInfoCarRentRequest;
import com.lzy.cloud.order.response.MemberToken;
import com.lzy.cloud.order.response.OrderInfoCarRentResponse;
import com.lzy.cloud.order.service.OrderInfoCarRentService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * @description:房车租赁订单子表
 * @author: liyun
 * @create: 2018-07-05 13:49
 */
@RestController
@RequestMapping("/order/car/rent")
public class OrderInfoCarRentController {
    @Autowired
    private OrderInfoCarRentService orderInfoCarRentService;
    @Autowired
    private OrderInfoFacade orderInfoFacade;
    @Autowired
    private BaseApi baseApi;

    /**
     * 根据订单id查询房车租赁订单
     * @param orderId
     * @return
     */
    @RequestMapping(value = "/find/{orderId}",method = RequestMethod.GET)
    public Result queryByOrderId(@PathVariable("orderId") String orderId) {
        Ensure.that(orderId).isNotNull("00010003");
        OrderInfoCarRentDto orderInfoCarRentDto = orderInfoCarRentService.queryByOrderId(orderId);
        return new Result(DozerUtils.map(orderInfoCarRentDto, OrderInfoCarRentResponse.class));
    }

    /**
     * 新增房车租赁订单
     * @param request
     * @return
     */
    @RequestMapping(value = "/create",method = RequestMethod.POST)
    public Result create(@RequestHeader("token") String token, @RequestBody OrderInfoCarRentRequest request) {
        Ensure.that(request).isNotNull("00000002");
/*        Ensure.that(token).isNotNull("00000002");
        MemberToken memberToken = DozerUtils.map(baseApi.isMemberLogin(token).getResult(),MemberToken.class);
        Ensure.that(memberToken).isNotNull("00001033");
        request.setCreateUser(memberToken.getId());
        request.setMemberId(memberToken.getId());*/
        request.setCreateUser(1L);
        request.setMemberId(1L);
        return new Result(orderInfoFacade.createOrder(DozerUtils.map(request, CarRentOrderDto.class)));
    }

    /**
     * 修改房车租赁订单
     * @param request
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result update(@RequestBody OrderInfoCarRentRequest request) {
        Ensure.that(request).isNotNull("00000002");
        boolean result = orderInfoCarRentService.update(DozerUtils.map(request, OrderInfoCarRentDto.class));
        Ensure.that(result).isTrue("00060003");
        return new Result(result);
    }

    /**
     * 查询房车租赁订单(分页)
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result selectList(OrderInfoCarRentRequest request) {
        Ensure.that(request).isNotNull("00010001");
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        PageInfo<OrderInfoCarRentDto> pageInfo = orderInfoCarRentService.selectList(pager, DozerUtils.map(request, OrderInfoCarRentDto.class));
        if (pageInfo != null && pageInfo.getList() != null) {
            pager.setTotal(pageInfo.getTotal());
            return new Result(pager, DozerUtils.maps(pageInfo.getList(), OrderInfoCarRentResponse.class));
        } else {
            return new Result();
        }
    }

    /**
     * 根据车辆id和租车时间查询订单
     * @param carId
     * @param rentTime
     * @return
     */
    @RequestMapping(value = "/find/by/car/id/and/rent/time",method = RequestMethod.POST)
    public Result findByCarIdAndRentTime(Long carId, String rentTime) {
        Ensure.that(carId).isNotNull("00000002");
        Ensure.that(rentTime).isNotNull("00000002");
        OrderInfoCarRentDto dto = orderInfoCarRentService.findByCarIdAndRentTime(carId, rentTime);
        Ensure.that(dto).isNotNull("00010007");
        Map<String, String> resultMap = new HashMap<String, String>();
        resultMap.put("startDate", dto.getStartDate());
        resultMap.put("endDate", dto.getEndDate());
        return new Result(resultMap);
    }


}
